'''
copyright: Copyright (C) 2015-2024, Wazuh Inc.

           Created by Wazuh, Inc. <info@wazuh.com>.

           This program is free software; you can redistribute it and/or modify it under the terms of GPLv2

type: integration

brief: These tests will check if the `interval` option of the Vulnerability Detector module
       is working correctly. This option is located in its corresponding section of
       the `ossec.conf` file and allows to define the time between scans.

components:
    - vulnerability_detector

suite: general_settings

targets:
    - manager

daemons:
    - wazuh-modulesd

os_platform:
    - linux

os_version:
    - Arch Linux
    - Amazon Linux 2
    - Amazon Linux 1
    - CentOS 8
    - CentOS 7
    - Debian Buster
    - Red Hat 8
    - Ubuntu Focal
    - Ubuntu Bionic

references:
    - https://documentation.wazuh.com/current/user-manual/capabilities/vulnerability-detection/index.html
    - https://documentation.wazuh.com/current/user-manual/reference/ossec-conf/vuln-detector.html#interval

tags:
    - general_settings
    - vulnerability_detector
'''
import pytest
from pathlib import Path

from wazuh_testing.constants.daemons import ANALYSISD_DAEMON, MODULES_DAEMON, SYSCHECK_DAEMON
from wazuh_testing.constants.paths.logs import WAZUH_LOG_PATH
from wazuh_testing.utils.callbacks import generate_callback
from wazuh_testing.tools.monitors.file_monitor import FileMonitor
from wazuh_testing.utils.db_queries import agent_db
from wazuh_testing.utils.configuration import get_test_cases_data, load_configuration_template
from wazuh_testing.modules.modulesd.vulnerability_detector import patterns as cb
from wazuh_testing.modules.modulesd.configuration import MODULESD_DEBUG
from wazuh_testing.modules.monitord.configuration import MONITORD_ROTATE_LOG
from . import TEST_CASES_PATH, CONFIGURATIONS_PATH


pytest.skip("The tests will be deprecated, they test the old Vulnerability Detector.", allow_module_level=True)

# Variables
local_internal_options = {MODULESD_DEBUG: '2', MONITORD_ROTATE_LOG: '0'}
daemons_handler_configuration = {'daemons': [ANALYSISD_DAEMON, MODULES_DAEMON, SYSCHECK_DAEMON]}
pytestmark = [pytest.mark.server]

# Configuration and cases data
configurations_path = Path(CONFIGURATIONS_PATH, 'configuration_interval.yaml')
cases_path = Path(TEST_CASES_PATH, 'cases_interval.yaml')

# test_interval_scan configurations
configuration_parameters, configuration_metadata, case_ids = get_test_cases_data(cases_path)
configurations = load_configuration_template(configurations_path, configuration_parameters,
                                             configuration_metadata)


@pytest.mark.tier(level=0)
@pytest.mark.parametrize('test_configuration, test_metadata', zip(configurations, configuration_metadata), ids=case_ids)
def test_interval_scan(test_configuration, test_metadata, set_wazuh_configuration, truncate_monitored_files,
                       configure_local_internal_options, clean_cve_tables_module, daemons_handler):
    '''
    description: Check that the time specified in the interval is correct at the time of sleeping the scan thread.

    test_phases:
        - setup:
            - Set a custom Wazuh configuration.
            - Configure custom local_internal_options.
            - Truncate wazuh logs.
            - Restart wazuh-modulesd daemon to apply configuration changes.
        - test:
            - Check in log that the scan sleeping time is the expected one.
        - teardown:
            - Truncate wazuh logs.
            - Restore initial configuration, both ossec.conf and local_internal_options.conf.

    wazuh_min_version: 4.2.0

    tier: 0

    parameters:
        - test_configuration:
            type: dict
            brief: Configuration loaded from `configuration_template`.
        test_metadata:
            type: dict
            brief: Test case metadata.
        - set_wazuh_configuration:
            type: fixture
            brief: Set wazuh configuration.
        - configure_local_internal_options:
            type: fixture
            brief: Set local_internal_options configuration.
        - truncate_monitored_files:
            type: fixture
            brief: Truncate all the log files and json alerts files before and after the test execution.
        - clean_cve_tables_module:
            type: fixture
            brief: Clean all CVE tables.
        - daemons_handler:
            type: fixture
            brief: Restart wazuh-modulesd daemon before starting a test, and stop it after finishing.

    assertions:
        - Check that the scan sleeping time is the expected one.

    input_description:
        - The `configuration_interval.yaml` file provides the module configuration for this test.
        - The `cases_interval` file provides the test cases.

    expected_output:
        - f".*wm_vuldet_run_sleep.*Sleeping for {num_seconds} seconds..."
    '''
    file_monitor = FileMonitor(WAZUH_LOG_PATH)

    file_monitor.start(callback=generate_callback(regex=cb.SCAN_INTERVAL_SLEEP,
                                                  replacement={"interval": test_metadata['expected_seconds']}))
    assert file_monitor.callback_result is not None, f"Unexpected '{cb.SCAN_INTERVAL_SLEEP}' event was found."
